set auto_path [concat ../../pkg/TMD  $auto_path]
package require TMD
source segment.tcl
proc save {fname str} {
    set f [open $fname w]
    puts $f $str
    close $f
}


proc getPDB { $pdbid } {
PDB create $pdbid
$pdbid fetch $pdbid
# change NH2 from hetatom to atom
$pdbid het2atom
$pdbid process
puts "$pdbid" 
puts "Has [$pdbid hasNmodel] model"
if { [$pdbid hasNmodel] > 1 } {
    puts "This is a NMR structure"
    set firstModel 1
} else {
    puts "THis is a X-Ray structure"
    # X-Ray structure has its first model index set as 0 instead
    set firstModel 0
}
foreach ssbond [$pdbid ssbonds] {
    puts "has ssbound: $ssbond"
}
foreach chain [$pdbid chains] {
    #get charmm readable pdb file
    # use getCoor instead for original pdb format
    puts "Chain $chain: [$pdbid chainlen $chain]"
    puts [$pdbid sequence $chain]
    puts "Saving chain $chain into file ${pdbid}_$chain.pdb"
    save ${pdbid}_$chain.pdb [$pdbid getCharmm $firstModel $chain]
}
}

proc orient { pdbid } {
    Charmm create $pdbid
    $pdbid do {
        load rtf ~/simulation/common/toph19_eef1.1.gouy.inp
        load para ~/simulation/common/param19_eef1.1.gouy.inp
    }
    $pdbid load psf pept.psf
    $pdbid load coor pept.crd
    $pdbid stream orient.inp
    $pdbid destroy
}

proc gen { name sequence } {
    Charmm create $name
    $name setup {
	cmd no
	output no
    }
    $name start
    $name do {
        load rtf ~/simulation/common/toph19_eef1.1.gouy.inp
        load para ~/simulation/common/param19_eef1.1.gouy.inp
    }
    $name setSolvent IMM1_water

    $name addseg pept $sequence
    puts "Generate $name:\n $sequence"
    puts "Making the structure helical"
    $name build2ndStruct pept alpha 1 end
    $name minimize sd 300
    puts "Writing structure to pept.psf and pept.crd"
    $name sendCmd "write psf card name pept.psf"
    $name sendCmd "write coor card name pept.crd"
    $name destroy
}

proc prep { pdbid } {
    PDB create pept
    puts "Fetching $pdbid..."
    if ![file exists $pdbid] { exec mkdir $pdbid }
    pept fetch $pdbid
    pept het2atom
    pept process
    puts "has [pept hasNmodel] models. Using the first model."
    if { [pept hasNmodel] > 1 } {
        set baseModel 1
    } else {
        set baseModel 0
    }
    set baseChain [lindex [pept chains] 0]
    foreach chain [pept chains] {
        puts "$chain [pept chainlen $chain] [pept sequence $chain]"
        set pdbfile [open $pdbid/${pdbid}_$chain.pdb w]
        puts -nonewline $pdbfile [pept getCharmm $baseModel $chain]
        close $pdbfile
    }
    puts "saving the $baseChain as monomer.pdb"
    set pdbfile [open $pdbid/monomer.pdb w]
    puts -nonewline $pdbfile [pept getCharmm $baseModel $baseChain]
    close $pdbfile

    foreach ssbond [pept ssbonds] {
        puts "has disulfide bond: $ssbond"
    }
    set base [pwd]
    Charmm create $pdbid
    $pdbid setup {
	cmd no
	output no
    }
    $pdbid start
    $pdbid do {
        load rtf ~/simulation/common/toph19_eef1.1.gouy.inp
        load para ~/simulation/common/param19_eef1.1.gouy.inp
    }
    $pdbid setSolvent IMM1_water

    $pdbid addseg pept monomer.pdb
    $pdbid sendCmd "write psf card name pept.psf"
    $pdbid sendCmd "write coor card name pept.crd"
    $pdbid destroy
    cd $base
    pept destroy
}
